package com.goldenautumn.prefabricate.activity.account

import android.graphics.Paint
import android.os.Handler
import android.os.Looper
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.user.LoginUiState
import com.goldenautumn.prefabricate.business.user.UserViewModel
import com.goldenautumn.prefabricate.databinding.ActivityVerificationBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA_PARAMS
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.goldenautumn.prefabricate.widget.verification.VerificationCodeView
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseActivity
import qncx.external.common.utils.intent
import qncx.external.common.utils.ticker
import qncx.external.common.widget.view.TipsDialog
import java.util.Date
import java.util.concurrent.TimeUnit

class VerificationActivity : BaseActivity<ActivityVerificationBinding>() {

    val type:Int
        get() = intent.getIntExtra(BUSINESS_PAGE_DATA,0)

    val account:String?
        get() = intent.getStringExtra(BUSINESS_PAGE_DATA_PARAMS)

    val area:String?
        get() = intent.getStringExtra("AREA")

    private var time = 60

    private lateinit var ticker: Channel<Long>

    private val mUserModel by viewModels<UserViewModel>()

    override fun getLayoutId(): Int = R.layout.activity_verification

    override suspend fun main() {
        initToolbarNav(mBinding.toolbar)

        Handler(Looper.getMainLooper()).postDelayed({
            KeyboardUtils.showSoftInput(mBinding.enterCode.editText)
        },500)

        mBinding.resend.setOnClickListener {
            time = 60
            mUserModel.getCode("$account")
        }

        mBinding.accountTv.text = "＋$area$account"

        mBinding.enterCode.setInputCompleteListener(object :
            VerificationCodeView.InputCompleteListener {

            override fun inputComplete() {
                val code = mBinding.enterCode.inputContent
                if (code.length == 6)
                    mUserModel.onPhoneLogin("$account",code)
            }

            override fun deleteContent() {

            }

        })

        ticker = ticker(TimeUnit.SECONDS.toMillis(1))

        while (isActive) {
            select {
                ticker.onReceive {
                    if(time == 0){
                        mBinding.resend.isEnabled = true
                        mBinding.resend.setText(R.string.get_verification)
                        mBinding.resend.setTextColor(ContextCompat.getColor(this@VerificationActivity,
                            qncx.external.common.R.color.colorPrimary))
                        mBinding.resend.paint.flags = Paint.UNDERLINE_TEXT_FLAG
                    } else {
                        --time
                        mBinding.resend.isEnabled = false
                        val format = TimeUtils.date2String(Date(time * 1000L),"mm:ss")
                        mBinding.resend.text = String.format(getString(R.string.login_send_resend),format)
                        mBinding.resend.setTextColor(ContextCompat.getColor(this@VerificationActivity,
                            qncx.external.common.R.color.color_666666))
                        mBinding.resend.paint.flags = 0
                        mBinding.resend.invalidate()
                    }
                }
                mUserModel.loginRequests.onReceive {
                    when (it) {
                        is LoginUiState.LoginSuccess -> {
                            ActivityUtils.finishActivity(LoginActivity::class.java)
                            finish()
                        }

                        is LoginUiState.LoginFail -> {
                            TipsDialog.Builder(this@VerificationActivity)
                                .setIcon(qncx.external.common.R.drawable.tips_warning_ic)
                                .setMessage(it.msg)
                                .show()
                        }

                        else -> {}
                    }
                }
            }
        }

    }

    override fun onStop() {
        super.onStop()
        ticker.cancel()
    }


}